using IService;
using Common;
using Model.Models;
using System;
using System.Collections.Generic;
using ModelDto;
using IRepository;
using Microsoft.EntityFrameworkCore;
using SqlserverManager;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;

namespace Service
{
    [Service]
    public class AccountRoleService : BaseService<AccountRole>, IAccountRoleService
    {
        private readonly IRoleRepository _RoleRepository;
        private readonly IDbConnectionFactory _IDbConnectionFactory;
        private readonly IRedisHelper _redisHelper;
        public AccountRoleService(IBaseRepository<AccountRole> BaseRepository,
            IDbConnectionFactory IDbConnectionFactory,
            IRoleRepository RoleRepository,
            IRedisHelper redisHelper
            ) : base(BaseRepository)
        {
            _IDbConnectionFactory = IDbConnectionFactory;
            _redisHelper = redisHelper;
            _RoleRepository = RoleRepository;
        }

        public async Task<bool> SaveRoleUser(string roleId, List<string> userIds, string accoutId)
        {
            var Context = _IDbConnectionFactory.CreateDbContext();

            DateTime now = DateTime.Now;

            var roleList = await QueryListAsync(a => a.RoleId == roleId);

            await Context.BeginTranAsync();
            try
            {
                if (roleList != null && roleList.Count > 0)
                {
                    await Context.Deleteable<AccountRole>(roleList).ExecuteCommandAsync();
                }
                if (userIds != null && userIds.Count > 0)
                {
                    List<AccountRole> addList = new List<AccountRole>();
                    foreach (var item in userIds)
                    {
                        AccountRole entity = new AccountRole();
                        entity.Id = Guid.NewGuid().ToString("N");
                        entity.CreateTime = now;
                        entity.CreateUser = accoutId;
                        entity.AccountId = item;
                        entity.RoleId = roleId;
                        entity.IsDelete = false;
                        entity.Remark = "";
                        addList.Add(entity);
                    }

                    await Context.Insertable<AccountRole>(addList).ExecuteCommandAsync();
                }

                await Context.CommitTranAsync();

                _redisHelper.DeleteKey($"{typeof(AccountRole).Name}-ALL");
            }
            catch (Exception ex)
            {
                await Context.RollbackTranAsync();

                return false;
            }

            return true;

        }

        public async Task<bool> DelleteRole(string roleId, string accoutId)
        {
            var Context = _IDbConnectionFactory.CreateDbContext();

            DateTime now = DateTime.Now;

            var role = await _RoleRepository.FindAsync(roleId);

            if (role == null)
            {
                return false;
            }
            role.IsDelete = true;
            role.DeleteUser = accoutId;
            role.DeleteTime = now;

            try
            {
                await Context.BeginTranAsync();

                await Context.Updateable<Role>(role).ExecuteCommandAsync();

                var roleList = await QueryListAsync(a => a.RoleId == roleId);

                if (roleList != null && roleList.Count > 0)
                {
                    await Context.Deleteable<Role>(roleList).ExecuteCommandAsync();
                }
                await Context.CommitTranAsync();

                _redisHelper.DeleteKey($"{typeof(AccountRole).Name}-ALL");
                _redisHelper.DeleteKey($"{typeof(Role).Name}-ALL");
            }
            catch (Exception ex)
            {
                await Context.RollbackTranAsync();

                return false;
            }

            return true;
        }
    }
}
